package com.apes.fn.scm.retailOrder.service.expense;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.scm.retailOrder.model.RetailOrder;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;

/**
 * 赋能门店接车赋能基地施工
 *
 * @author wuq
 * @create 2020-04-20 17:19
 */
public class FnBase2FnBase extends AbstractRetailOrderFnExpense {

    private PartyRole platformCompany;
    private PartyRole fnBase;
    private Dept dept;
    private RetailOrder retailOrder;

    public FnBase2FnBase(PartyRole platformCompany, PartyRole fnBase,
                         double amount, String origin, String originBusinessType, String source, String sourceBusinessType,
                         Dept dept, String carLicense, Enumeration caseType) {
        super(amount, origin, originBusinessType, source, sourceBusinessType, carLicense, caseType);
        this.platformCompany = platformCompany;
        this.fnBase = fnBase;
        this.dept = dept;
    }

    public FnBase2FnBase(PartyRole platformCompany, RetailOrder retailOrder) {
        this(platformCompany, retailOrder.getServiceDept().getCompany(), retailOrder.getAmtTotal(),
                retailOrder.getSource(), retailOrder.getSourceBusinessType(), retailOrder.getId(), retailOrder.getClass().getSimpleName(),
                retailOrder.getServiceDept(), retailOrder.getCustomerExpand().getCarLicense(), retailOrder.getCaseType());
        this.retailOrder = retailOrder;
    }

    /**
     * 赋能基地接车赋能接地施工
     * 这种情况只存在定损费、平台服务费
     *
     * @param condition 条件策略
     */
    @Override
    public void create(JSONObject condition) {
        String type = getCaseType(super.getCaseType().getId());
        double lossQuotationExpense = 0d;
        switch (type) {
            case "code":    // 代码单
                lossQuotationExpense = condition.getDouble(ExpenseEnum.codeOrderLossQuotationExpense.getCode());
                break;
            case "notCode":    // 非代码单
                lossQuotationExpense = condition.getDouble(ExpenseEnum.notCodeOrderLossQuotationExpense.getCode());
                break;
            case "push":    // 推送
                lossQuotationExpense = condition.getDouble(ExpenseEnum.pushOrderLossQuotationExpense.getCode());
                break;
        }
        createLossQuotationExpense(lossQuotationExpense);
        createPlatformExpense(0d);
    }

    @Override
    public void createInfoExpense(double expense) {
    }

    @Override
    public void createPlatformExpense(double expense) {
        double platformFreeRatio = getPlatformFreeRatio(fnBase);    // 获取基地平台管理费率
        createExpense(fnBase, platformCompany, super.getAmount() * platformFreeRatio, ExpenseEnum.platformExpense.getCode(), null, dept, "");
    }

    @Override
    public void createLossQuotationExpense(double expense) {
        createExpense(fnBase, platformCompany, super.getAmount() * expense, ExpenseEnum.lossQuotationExpense.getCode(), null, dept, "");

        // 根据定损费按照平台服务费率退换给基地
        double platformFreeRatio = getPlatformFreeRatio(fnBase);    // 获取基地平台管理费率
        createExpense(platformCompany, fnBase, super.getAmount() * expense * platformFreeRatio, ExpenseEnum.platformExpense.getCode(), dept, null, "平台退回基地定损信息费的平台服务费。");
    }

}
